import os, os.path, shutil, hashlib
from PyQt4.QtCore   import QUrl
#from PyQt4.QtGui    import *
#from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import QNetworkReply

from wedewoba.PageBrowser import PageBrowser

class AbstractReply(object):

    def __init__(self, reply, tmpDir):
        self.url = QUrl(reply.url())
        self.md5 = hashlib.md5()
        self.md5.update(self.url.toString())
        self.filePath = tmpDir+"/"+self.md5.hexdigest()
        with open(self.filePath, "w+") as f:
            f.write(reply.readAll())

        self.headers = {}
        for h in reply.rawHeaderList():
            self.headers[str(h)] = str(reply.rawHeader(h))

    def read(self):
        with open(self.filePath, "r+") as f:
            return f.read()

    def readAll(self):
        return self.read()


class PageDownloader(PageBrowser):

    
    def __init__(self, parent=None):
        super(PageDownloader, self).__init__()
        self.replys = []


    def preLoad(self):
        self.bdir = str(self.getDomain(self.url))
        if os.path.isdir(self.bdir):
            shutil.rmtree(self.bdir)
        os.mkdir(self.bdir)
        os.mkdir(self.bdir+"/tmp")
        self.replys = []
        nam = self.NetworkAccessManager
        def rf(r):
            print type(QNetworkReply), type(r)
            #if r.__class__.__name__QNetworkReply):
            self.replys.append(AbstractReply(r, self.bdir+"/tmp"))
        nam.finished.connect(rf)

    def onloadFinished(self, result):
        mainDomain = self.getDomain(self.url)
        cTypeReplys = {}
        for r in self.replys:
            if self.getDomain(r.url) == mainDomain and "Content-Type" in r.headers:
                v = str(r.headers["Content-Type"].split(";")[0]).lower()
                if v not in cTypeReplys:
                    cTypeReplys[v] = []
                cTypeReplys[v].append(r)
        for ctype, replys in cTypeReplys.items():
            urls = []
            for r in replys:
                urls.append(r.url)
            pathes = [ u.path() for u in urls ]
            #print pathes
            print ctype
            print "-"*10
            print [os.path.dirname(str(p)) for p in pathes]
            print "-"*10
            print os.path.commonprefix([str(p) for p in pathes])
            print "-"*40
 
    def getDomain(self, url):
        d = url.host().split('.')
        return d[-2]+"."+d[-1]
        

